{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2,math\n",
    "def guideFilter(I, p, winSize, eps):\n",
    "    \"\"\"\n",
    "    导向图像(Guidance Image) I，滤波输出图像(Filtering Input Image) p，均值平滑窗口半径 r，正则化参数 e。\n",
    "    利用导向滤波进行图像平滑处理时，通常令p=I。\n",
    "    其中：guideFilter(）函数调用opencv自带的库函数blur() 进行均值平滑。\n",
    "    :param I:\n",
    "    :param p:\n",
    "    :param winSize:\n",
    "    :param eps:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    \n",
    "    sqaure_avg = cv2.blur(p * p, winSize)\n",
    "    avg = cv2.blur(p, winSize)\n",
    "    \n",
    "    ak = (sqaure_avg - avg ** 2)/(sqaure_avg - avg **2 + eps)\n",
    "    bk = (-1 * ak + 1) * avg\n",
    "    \n",
    "    a = cv2.blur(ak, winSize)\n",
    "    b = cv2.blur(bk, winSize)\n",
    "    \n",
    "    q = a * p + b\n",
    "    \n",
    "    return q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "face.jpg\n",
      "(777, 600, 3)\n",
      "0.1521372\n"
     ]
    }
   ],
   "source": [
    " \n",
    "\n",
    "\n",
    "def fastGuideFilter(I, p, winSize, eps, s):\n",
    "    \"\"\"\n",
    "    导向图像(Guidance Image) I，滤波输出图像(Filtering Input Image) p，正则化参数 eps。\n",
    "    利用导向滤波进行图像平滑处理时，通常令p=I。\n",
    "    其中：guideFilter(）函数调用opencv自带的库函数blur() 进行均值平滑。\n",
    "    :param I:\n",
    "    :param p:\n",
    "    :param winSize:\n",
    "    :param eps:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    \n",
    "    sp = cv2.resize(p, None, fx=s, fy=s)\n",
    "    \n",
    "    \n",
    "    sqaure_avg = cv2.blur(sp * sp, winSize)\n",
    "    avg = cv2.blur(sp, winSize)\n",
    "    \n",
    "    ak = (sqaure_avg - avg ** 2)/(sqaure_avg - avg **2 + eps)\n",
    "    bk = (-1 * ak + 1) * avg\n",
    "    \n",
    "    a = cv2.blur(ak, winSize)\n",
    "    b = cv2.blur(bk, winSize)\n",
    "    \n",
    "    bigA = cv2.resize(a, None, fx=2, fy=2)\n",
    "    bigB = cv2.resize(b, None, fx=2, fy=2) \n",
    "    q = bigA * p + bigB\n",
    "    \n",
    "    return q\n",
    "\n",
    "\"\"\"\n",
    "下图导向滤波采用了r=16也就是winSize=(16,16), eps=0.01的参数大小。  \n",
    "快速导向滤波采用了r=16也就是winSize=(16,16), eps=0.01，s=0.5的参数大小。\n",
    "\"\"\"\n",
    "def run():\n",
    "    name = input()\n",
    "    image = cv2.imread(name, cv2.IMREAD_ANYCOLOR)\n",
    "    #将图像归一化\n",
    "    \n",
    "    # time start\n",
    "    t1 = cv2.getTickCount()\n",
    "    image_0_1 = image/255.0\n",
    " \n",
    "    #导向滤波(三通道)\n",
    "    b, g, r = cv2.split(image_0_1)\n",
    "    gf1 = guideFilter(b, b, (16,16), math.pow(0.1,2))\n",
    "    gf2 = guideFilter(g, g, (16,16), math.pow(0.1,2))\n",
    "    gf3 = guideFilter(r, r, (16,16), math.pow(0.1,2))\n",
    "\n",
    "#     gf1 = FastguideFilter(b, b, (16, 16), math.pow(0.1, 2),s=0.5)\n",
    "#     gf2 = FastguideFilter(g, g, (16, 16), math.pow(0.1, 2),s=0.5)\n",
    "#     gf3 = FastguideFilter(r, r, (16, 16), math.pow(0.1, 2),s=0.5)\n",
    "    gf = cv2.merge([gf1, gf2, gf3])\n",
    " \n",
    "    print(gf.shape)\n",
    " \n",
    "    \n",
    "    \n",
    "    \n",
    "\n",
    "    #保存导向滤波结果\n",
    "    gf = gf*255\n",
    "    gf[gf>255] = 255\n",
    "    gf = np.round(gf)\n",
    "    gf = gf.astype(np.uint8)\n",
    "    res = np.hstack((image,gf))\n",
    "    \n",
    "    # time end\n",
    "    t2 = cv2.getTickCount()\n",
    " \n",
    "    # 计算执行秒数,利用getTickFrequency()获取时钟频率\n",
    "    t = (t2 - t1) / cv2.getTickFrequency()\n",
    "    print(t)\n",
    "    \n",
    "    cv2.imshow(\"res\",res)\n",
    "    cv2.waitKey(0)\n",
    "    return gf\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "gf = run()\n",
    " "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
